﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Wizard.UI;
using RoketPack.Management;
using System.Xml;
using System.Threading;

namespace RoketPack
{
    public partial class RepositoryInstallationWizard : WizardSheet
    {
        private string p_RepositoryUrl = null;
        private string p_RepositoryName = null;
        private string p_RepositoryDescription = null;
        private Manager m_Manager = null;
        private bool p_InstalledRepository = false;

        public RepositoryInstallationWizard(Manager manager, string url)
        {
            InitializeComponent();
            this.TopMost = true;

            this.m_Manager = manager;

            // Grab the repository information.
            ConfigurationReader c = new ConfigurationReader();
            Repository r = c.GetRepository(new Uri(url));
            this.p_RepositoryUrl = r.Url;
            this.p_RepositoryName = r.Name;
            this.p_RepositoryDescription = r.Description;

            this.Pages.Add(new RepositoryUI.InstallPage(this));
        }

        internal string RepositoryUrl
        {
            get
            {
                return this.p_RepositoryUrl;
            }
            set
            {
                this.p_RepositoryUrl = value;
            }
        }

        internal string RepositoryName
        {
            get
            {
                return this.p_RepositoryName;
            }
            set
            {
                this.p_RepositoryName = value;
            }
        }

        internal string RepositoryDescription
        {
            get
            {
                return this.p_RepositoryDescription;
            }
            set
            {
                this.p_RepositoryDescription = value;
            }
        }

        internal bool InstalledRepository
        {
            get
            {
                return this.p_InstalledRepository;
            }
        }

        internal void InstallRepository(Repository repo)
        {
            try
            {
                // Add the new repository.
                this.m_Manager.Repositories.Add(repo);

                // Write the repository information back to disk.
                XmlWriterSettings settings = new XmlWriterSettings();
                settings.Indent = true;
                using (XmlWriter writer = XmlWriter.Create("Repositories.xml", settings))
                {
                    writer.WriteStartElement("repositories");

                    foreach (Repository r in this.m_Manager.Repositories)
                    {
                        writer.WriteStartElement("repo");
                        writer.WriteAttributeString("url", r.Url);
                        writer.WriteStartElement("name");
                        writer.WriteString(r.Name);
                        writer.WriteEndElement();
                        writer.WriteStartElement("description");
                        writer.WriteString(r.Description);
                        writer.WriteEndElement();
                        writer.WriteEndElement();
                    }

                    writer.WriteEndElement();
                    writer.Close();
                }

                // Get the manager to reload it's list of repositories.
                this.m_Manager.ReinitalizeRepositories();
                
                this.p_InstalledRepository = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show("Unable to add repository.");
            }
        }
    }
}
